package cn.mesmile.clickhouse.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.apache.ibatis.type.Alias;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Id;
import javax.persistence.Table;
import java.sql.SQLException;
import java.util.Date;

/**
 * @author zb
 * @date 2021/6/25 23:26
 * @Description
 */
@Alias("Book")
@Table(name = "book")
@Data
public class Book {

    @Id
    private Long id;

    private String name;

    private String author;

    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date publishDate;

    private Integer price;

    private Boolean isPublish;

    public void test() throws SQLException {
        // ① 加载驱动
//        InputStream inputStream = Thread.currentThread()
//                .getContextClassLoader().getResourceAsStream("druid.properties");
//        Properties properties = new Properties();
//        properties.load(inputStream);
        // DruidDataSourceFactory.createDataSource(properties);
//        DataSource dataSource = new HikariDataSource();
//        // ② 获取连接
//        Connection connection = dataSource.getConnection();
//        // ③ 预编译
//        PreparedStatement preparedStatement = connection.prepareStatement("select * from book");
//        // ④ 执行sql
//        ResultSet resultSet = preparedStatement.executeQuery();
//
//        // 业务
//
//        // ⑤ 释放连接，connection对象使用完毕，归还连接到连接池
//        connection.close();
        /**
         * 【一个mysql服务端最多支持 1024个连接】
         * spring:
         *   datasource:
         *     type: com.alibaba.druid.pool.DruidDataSource
         *     click:
         *       driverClassName: ru.yandex.clickhouse.ClickHouseDriver
         *       url: jdbc:clickhouse://127.0.0.1:8123/default
         *       initialSize: 10 // 初始化连接数量
         *       maxActive: 100 // 最大激活数量
         *       minIdle: 10 // 最小空闲数量
         *       maxWait: 6000 // 获取连接的最大等待时间 6 秒
         *       validationQuery: select 1  // 判断连接是否有效的sql语句
         *       testOnBorrow: false  // 从连接池中拿连接的时候是否验证连接有效
         *       testOnReturn: false  // 连接使用完之后归还到连接池后是否校验连接有效
         *       testWhileIdle: true  // 空闲的时候校验连接是否有效
         *
         *       @Configuration
         * public class DruidConfig {
         *     @Resource
         *     private JdbcParamConfig jdbcParamConfig ;
         *     @Bean
         *     public DataSource dataSource() {
         *         DruidDataSource datasource = new DruidDataSource();
         *         datasource.setUrl(jdbcParamConfig.getUrl());
         *         datasource.setDriverClassName(jdbcParamConfig.getDriverClassName());
         *         datasource.setInitialSize(jdbcParamConfig.getInitialSize());
         *         datasource.setMinIdle(jdbcParamConfig.getMinIdle());
         *         datasource.setMaxActive(jdbcParamConfig.getMaxActive());
         *         datasource.setMaxWait(jdbcParamConfig.getMaxWait());
         *         return datasource;
         *     }
         * }
         *
         *
         * @Component
         * @ConfigurationProperties(prefix = "spring.datasource.click")
         * public class JdbcParamConfig {
         *     private String driverClassName ;
         *     private String url ;
         *     private Integer initialSize ;
         *     private Integer maxActive ;
         *     private Integer minIdle ;
         *     private Integer maxWait ;
         *     // 省略 GET 和 SET
         * }
         */
    }
}
